<?php

namespace app\model;

class LoginCommon extends Common
{
    private static $obj;

    public function _initialize()
    {
        parent::_initialize();

    }

    /**
     * 类入口
     * @return LoginCommon
     */
    public static function getInstance()
    {
        if (is_null(self::$obj))
            self::$obj = new self();
        return self::$obj;
    }

    /**
     * 检查手机号码是否存在
     * @param $str_mobile
     * @param $str_key
     * @return bool|int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkIsSetAccount($str_mobile, $str_key = 'mobile')
    {
        $arr_info = $this->toFind([$str_key => $str_mobile], 'id');
        if (isset($arr_info['id'])) {
            return (int)$arr_info['id'];
        }
        return false;
    }

    /**
     * 设置登录密码
     * @param $int_user_id
     * @param $str_password
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function setLoginPassword($int_id, $str_password)
    {
        $str_pwd_key = $this->_getRandomString(8);
        $arr_data = [
            'password' => passwordEncryption($str_password, $str_pwd_key),
            'pwd_key' => $str_pwd_key,
        ];

        $this->toUpdate(['id' => $int_id], $arr_data);

        return true;
    }

    /**
     * 检查是否设置登录密码
     * @param $int_user_id
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkIsSetLoginPwd($int_id)
    {
        $arr_info = $this->toFind(['id' => $int_id], 'password');
        if (isset($arr_info['password']) && empty($arr_info['password']) === false) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 更新登录密码
     * @param $int_id
     * @param $str_id_name
     * @param $str_password
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function updateLoginPassword($int_id, $str_password)
    {
        $str_pwd_key = _getRandomString(8);
        $this->toUpdate(['id' => $int_id], ['password' => passwordEncryption($str_password, $str_pwd_key), 'pwd_key' => $str_pwd_key]);
    }

    /**
     * 检查登录密码是否正确
     * @param $int_id
     * @param $str_password
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkLoginPasswordIsRight($int_id, $str_password)
    {
        $arr_info = $this->toFind(['id' => $int_id], 'password,pwd_key');

        if (!isset($arr_info['password'])) return false;

        if (passwordEncryption($str_password, $arr_info['pwd_key']) == $arr_info['password']) {
            return true;
        }

        return false;
    }

    /**
     * 设置支付密码
     * @param $int_id
     * @param $str_pay_password
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function setPayPassword($int_id, $str_pay_password)
    {
        $arr_data = [
            'pay_password' => passwordEncryption($str_pay_password, $int_id),
        ];

        $this->toUpdate(['id' => $int_id], $arr_data);

        return true;
    }

    /**
     * 检查是否设置支付密码
     * @param $int_id
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkIsSetPayPassword($int_id)
    {
        $arr_info = $this->toFind(['id' => $int_id], 'pay_password');
        if (isset($arr_info['pay_password']) && empty($arr_info['pay_password']) === false) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 检查支付密码是否正确
     * @param $int_id
     * @param $str_pay_password
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkPayPasswordIsRight($int_id, $str_pay_password)
    {
        $arr_info = $this->toFind(['id' => $int_id], 'pay_password');

        if (!isset($arr_info['pay_password'])) return false;

        if (passwordEncryption($str_pay_password, $int_id) == $arr_info['pay_password']) {
            return true;
        }

        return false;
    }
}
